﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace RealState.Utils
{
    public class JavaScript
    {
        public static void Alert(string message, Page reference)
        {
            string format = "alert(\"{0}\");";
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("Admin/default.aspx?m="))
            {
                format = "$(\"<div><p>{0}</p></div>\").dialog({{autoOpen:true, modal:true, resizable:false, title:'oneCMS message'}});";
            }
            string script = string.Format(format, message.Replace("\"", "'").Replace("\n", @"\n").Replace("\r", ""));
            ScriptManager.RegisterStartupScript(reference, reference.GetType(), "alert", script, true);
        }

        public static void Error(string message, Page reference) {
            
            string cleanMessage = message.Replace("'", "\'");
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("$().toastmessage('showToast', {");
            builder.AppendLine("text : '" + cleanMessage + "',");
            builder.AppendLine("sticky : true,position : 'top-right',type : 'error',closeText : '',");
            builder.AppendLine("close : function () {console.log('message is closed ...');}});");

            ScriptManager.RegisterStartupScript(reference, reference.GetType(), "showStickyErrorToast", builder.ToString(), true);

         
        }

        public static void Success(string message, Page reference, string urlToRedirect)
        {
            string cleanMessage = message.Replace("'", "\'");
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("$().toastmessage('showToast', {");
            builder.AppendLine("text : '" + cleanMessage + "',");
            builder.AppendLine("sticky : true,position : 'top-right',type : 'success',closeText : '',");
            builder.AppendLine("close : function () {console.log('message is closed ...');}});");
            builder.AppendLine("setTimeout(\"window.location.href='" + urlToRedirect.Trim() + "'\",3000);");
            ScriptManager.RegisterStartupScript(reference, reference.GetType(), "showSuccessToast", builder.ToString(), true);
        }

        public static void AlertAndRedirect(string message, string urlToRedirect, Page reference)
        {
            string format = "$(\"<div><p>{0}</p></div>\").dialog({{autoOpen:true, modal:true, resizable:false, title:'oneCMS message', close:function(event,ui) {{{1}}} }});";
            string str2 = "window.location.href='" + urlToRedirect.Trim() + "';";
            string script = string.Format(format, message.Replace("\"", "'").Replace("\n", @"\n").Replace("\r", ""), str2);
            ScriptManager.RegisterStartupScript(reference, reference.GetType(), "alert_redirect", script, true);
        }

        public static void ReloadPage(Page reference)
        {
            ScriptManager.RegisterStartupScript(reference, reference.GetType(), "reload", "window.location.href = window.location.href;", true);
        }
    }
}
